package lambda;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.*;
import java.util.stream.*;

/**
 * Stream流API
 */
public class LambdaStream {

    @Test
    public void test1() {
        List<People> list = Lists.newArrayList(
                new People(2,"201",1000d,"1",Lists.newArrayList("tom")),
                new People(1,"102",10010d,"2",Lists.newArrayList("jack","lucy")),
                new People(1,"101",20001d,"3",Lists.newArrayList("jackson","liLei")));

        // filter  过滤
        list = list.stream().filter(p->Objects.equals(1,p.getId())).collect(Collectors.toList());

        // map  映射转换
        List<Integer> mapList = list.stream().map(People::getId).collect(Collectors.toList());

        // mapToInt 映射转换后累加  类似函数 mapToLong  mapToDouble
        int sum = list.stream().mapToInt(People::getId).sum();

        // flatMap 将集合元素平铺映射  类似函数 flatMapToInt flatMapToLong flatMapToDouble
        List<String> childrenList = list.stream().map(People::getChildren).flatMap(Collection::stream).collect(Collectors.toList());
        System.out.println("子元素1："+childrenList);
        childrenList = list.stream().flatMap(p->p.getChildren().stream()).collect(Collectors.toList());
        System.out.println("子元素2："+childrenList);

        // distinct 去除重复 根据元素的equals与hashcode去重
        list = list.stream().distinct().collect(Collectors.toList());

        // sorted 排序 根据元素Comparable#compareTo方法排序
        list = list.stream().sorted().collect(Collectors.toList());

        // sorted 排序 根据指定比较器Comparator排序
        list = list.stream().sorted(Comparator.comparing(People::getId)).collect(Collectors.toList());

        // min 最小值
        list.stream().min(Comparator.comparing(People::getId)).ifPresent(min-> System.out.println("最小值："+min));

        // max 最大值
        list.stream().max((o1, o2) -> o2.getId().compareTo(o1.getId())).ifPresent(min-> System.out.println("最小值："+min));

        // peek 和 map的区别 返回执行前的Stream流，不会返回新的流
        list = list.stream().peek(p-> System.out.println(p.getName())).collect(Collectors.toList());

        // limit 步长 类似pageSize
        list = list.stream().limit(2).collect(Collectors.toList());

        // skip 从那个下标index开始计数
        list = list.stream().skip(1).collect(Collectors.toList());

        // skip limit 内存分页
        int pageNo = 2,pageSize = 10;
        list = list.stream().skip((pageNo-1)*pageSize).limit(pageSize).collect(Collectors.toList());

        // forEach 循环调用，有序执行
        list.forEach(System.out::println);

        // forEachOrdered 并行执行，如果使用forEach不一定保证顺序，保证顺序需要使用forEachOrdered
        list.parallelStream().forEachOrdered(System.out::println);

        // reduce 累积
        int total = list.stream().map(People::getId).reduce(Integer::sum).orElse(0);

        // reduce 带初始值的累积
        total = list.stream().map(People::getId).reduce(0,Integer::sum);

        // count 统计集合数量 == list.size()
        long sumTotal = IntStream.rangeClosed(1,5).count();

        // anyMatch list中的元素任何一个能够匹配为true
        boolean isAnyMatch = list.stream().anyMatch(item -> Objects.equals(item.getId(), 1));

        // noneMatch list中的元素所有元素都不能匹配为true
        boolean noneMatch = list.stream().noneMatch(item -> Objects.equals(item.getName(), "d"));

        // allMatch list中的元素所有元素都能匹配为true
        boolean allMatch = list.stream().allMatch(item -> Objects.equals(item.getName(), "d"));

        // 创建一个数据流
        Stream.of("1","2","3").forEach(System.out::println);

    }


}
